package com.helps.interceptor;

import com.helps.db.Database;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class ApiInterceptor implements HandlerInterceptor {

    private Database database;

    @Resource(name = "database")
    public void setDatabase(Database database) {
        this.database = database;
    }

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        System.out.println("ApiInterceptor");

        Map<String, String> account = database.fetchOne("select * from accounts where account_id = 25");
        System.out.println(account);

        //response.getWriter().write("error");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object handler, Exception ex)
            throws Exception {

    }
}
